
<!--
All rights Reserved, Designed By www.youyacao.com
@Description:产品更新列表
@author:成都市一颗优雅草科技有限公司
@version V4.2
注意：本前端源码遵循 MIT开源协议本内容仅限于个人参考，禁止用于其他的商业用途
需要商业用途或者定制开发等可访问songshu.youyacao.com   联系QQ:422108995 23625059584
开源gitee地址：https://gitee.com/youyacao/songshu-video-page
 -->



<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-cascades"></i> 用户列表
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-button
          type="primary"
          icon="el-icon-delete"
          class="handle-del mr10"
          @click="delAllSelection"
        >批量删除
        </el-button>
        <!-- <el-select v-model="query.address" placeholder="地址" class="handle-select mr10">
          <el-option key="1" label="广东省" value="广东省"></el-option>
          <el-option key="2" label="湖南省" value="湖南省"></el-option>
        </el-select>-->
        <!--<el-input v-model="query.name" placeholder="用户名/用户ID/手机号" class="handle-input mr10"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>-->
        <el-button
          style="float: right;margin-bottom:20px; "
          type="primary"
          icon="el-icon-lx-add"
          class="handle-del mr10"
          @click="addUpdate"
        >发布新版本
        </el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        header-cell-class-name="table-header"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="id" label="ID" width="55" align="center"></el-table-column>
        <el-table-column prop="version" label="版本号"></el-table-column>
        <el-table-column prop="appid" label="APPID"></el-table-column>
        <el-table-column prop="content" label="更新内容"></el-table-column>
        <el-table-column prop="time" label="更新时间"></el-table-column>
        <el-table-column prop="ios_download" label="苹果下载地址"></el-table-column>
        <el-table-column prop="android_download" label="安卓下载地址"></el-table-column>
        <el-table-column label="下载方式" align="center">
          <template slot-scope="scope">
            {{scope.row.open_type==1?'直接下载':'浏览器打开'}}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              @click="handleEdit(scope.$index, scope.row)"
            >编辑
            </el-button>
            <el-button
              type="text"
              icon="el-icon-delete"
              class="red"
              @click="handleDelete(scope.$index, scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          layout="total, prev, pager, next"
          :current-page="query.page"
          :page-size="query.pageSize"
          :total="total"
          @current-change="handlePageChange"
        ></el-pagination>
      </div>
    </div>
    <!-- 添加弹出框 -->
    <el-dialog title="添加" :visible.sync="addVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="版本号">
          <el-input v-model="form.version"></el-input>
        </el-form-item>
        <el-form-item label="APPID">
          <el-input v-model="form.appid"></el-input>
        </el-form-item>
        <el-form-item label="更新内容">
          <el-input type="textarea"
                    autosize
                    placeholder="请输入更新内容" v-model="form.content "></el-input>
        </el-form-item>
        <el-form-item label="苹果下载地址">
          <el-input v-model="form.ios_download"></el-input>
        </el-form-item>
        <el-form-item label="安卓下载地址">
          <el-input v-model="form.android_download"></el-input>
        </el-form-item>
        <el-form-item label="下载方式">
          <el-tooltip class="item" effect="dark" content="IOS默认为外部浏览器打开" placement="right">
            <el-select v-model="form.open_type" placeholder="下载方式" class="handle-select mr10">
              <el-option key="直接下载" label="直接下载" value="1"></el-option>
              <el-option key="浏览器打开" label="浏览器打开" value="2"></el-option>
            </el-select>
          </el-tooltip>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUpdateSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑弹出框 -->
    <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
      <el-form ref="form" :model="form" label-width="70px">
        <el-form-item label="版本号">
          <el-input disabled v-model="form.version"></el-input>
        </el-form-item>
        <el-form-item label="APPID">
          <el-input v-model="form.appid"></el-input>
        </el-form-item>
        <el-form-item label="更新内容">
          <el-input type="textarea"
                    autosize
                    placeholder="请输入更新内容" v-model="form.content "></el-input>
        </el-form-item>
        <el-form-item label="苹果下载地址">
          <el-input v-model="form.ios_download"></el-input>
        </el-form-item>
        <el-form-item label="安卓下载地址">
          <el-input v-model="form.android_download"></el-input>
        </el-form-item>
        <el-form-item label="下载方式">
          <el-tooltip class="item" effect="dark" content="IOS默认为外部浏览器打开" placement="right">
            <el-select v-model="form.open_type" placeholder="下载方式" class="handle-select mr10">
              <el-option key="直接下载" label="直接下载" value="1"></el-option>
              <el-option key="浏览器打开" label="浏览器打开" value="2"></el-option>
            </el-select>
          </el-tooltip>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
    export default {
        name: 'update',
        data () {
            return {
                addVisible:false,
                query: {
                    name: '',
                    page: 1,
                    pageSize: 10
                },
                tableData: [],
                multipleSelection: [],
                delList: [],
                editVisible: false,
                total: 0,
                form: {},
                idx: -1,
                id: -1
            }
        },
        created () {
            this.getData()
        },
        methods: {
            // 获取 easy-mock 的模拟数据
            getData () {
                this.$api.update.list(this.query).then(res => {
                    this.tableData = res.data
                    this.total = res.total || 0
                })
            },
            // 触发搜索按钮
            handleSearch () {
                this.$set(this.query, 'page', 1)
                this.getData()
            },
            // 删除操作
            handleDelete (index, row) {
                // 二次确认删除
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                        .then(() => {

                            var arr = [row.id]
                            this.$api.update.delete({ids: arr}).then(res => {
                                if (res.code == 1) {
                                    this.$message.error('删除失败,'+res.msg)
                                    return
                                }
                                this.getData()
                                this.$message.success('删除成功')
                            })
                        })
                        .catch(() => {
                        })
            },
            // 多选操作
            handleSelectionChange (val) {
                this.multipleSelection = val
            },
            delAllSelection () {
                const length = this.multipleSelection.length
                var arr = []
                this.delList = this.delList.concat(this.multipleSelection)
                for (let i = 0; i < length; i++) {
                    arr.push(this.multipleSelection[i].id)
                }
                this.$confirm('确定要删除吗？', '提示', {
                    type: 'warning'
                })
                        .then(() => {
                            this.$api.update.delete({ids: arr}).then(res => {
                                if (res.code == 1) {
                                    this.$message.error('删除失败,'+res.msg)
                                    return
                                }
                                this.getData()
                                this.$message.success('删除成功')
                            })
                        })
                        .catch(() => {
                        })
                this.multipleSelection = []
            },
            // 编辑操作
            handleEdit (index, row) {
                this.idx = index
                this.form = row
                this.editVisible = true
            },
            // 保存编辑
            saveEdit () {
                this.$api.update.update(this.form).then((res) => {
                    if (res.code == 1) {
                        this.$message.error(`修改失败,`+res.msg)
                        return
                    }
                    this.editVisible = false
                    this.$message.success(`修改成功`)
                    this.$set(this.tableData, this.idx, this.form)
                })

            },
            // 分页导航
            handlePageChange (val) {
                this.$set(this.query, 'page', val)
                this.getData()
            },
            uploadSuccess (response, file, fileList) {
                if (response.code === 0) {
                    this.form.head_img = response.data.url
                    this.fileList = []
                } else {
                    this.$message.error('上传失败,' + response.msg)
                }

            },
            addUpdate () {
                this.form = {}
                this.addVisible = true
            },
            addUpdateSubmit(){
                this.$api.update.add(this.form).then((res)=>{
                    this.addVisible = false
                    if(res.code==1){

                        this.$message.error(res.msg);
                        return;
                    }
                    this.$message.success(`添加成功`)
                    this.getData();
                });
            }
        }
    }
</script>

<style scoped>
  .handle-box {
    margin-bottom: 20px;
  }

  .handle-select {
    width: 120px;
  }

  .handle-input {
    width: 300px;
    display: inline-block;
  }

  .table {
    width: 100%;
    font-size: 14px;
  }

  .red {
    color: #ff0000;
  }

  .mr10 {
    margin-right: 10px;
  }

  .table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
  }

  .type-icon {
    display: block;
    width: 150px;
    height: 150px;
  }
</style>
